package com.xbongbong.saas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 电话号码解释枚举
 * @date 2019/4/8 19:42
 * @since v1.0
 */
@Getter
public enum PhoneExtEnum {
    WORK("enum.phoneExt.name.work","1"),


    TEL("enum.phoneExt.name.tel","2"),


    ADDRESS("enum.phoneExt.name.address","3"),


    MAIN("enum.phoneExt.name.main","4"),


    FAX("enum.phoneExt.name.fax","5"),


    OTHER("enum.phoneExt.name.other","6");

    /** 枚举值 -- */
    private String name;
    /** 枚举信息--中文名称 */
    private String code;

    PhoneExtEnum(String name, String code) {
        this.name = name;
        this.code = code;
    }

    /**
     * 获取全部枚举
     *
     * @return List<PhoneExtEnum>
     */
    public static List<PhoneExtEnum> getAllEnum() {
        List<PhoneExtEnum> list = new ArrayList<>();
        for (PhoneExtEnum cache : values()) {
            list.add(cache);
        }
        return list;
    }

    /**
     * 根据alias获取枚举
     * @return
     */
    public static PhoneExtEnum getByCode(String code) {
        if (code == null) {
            return PhoneExtEnum.OTHER;
        }
        for (PhoneExtEnum cache : values()) {
            if (Objects.equals(code, cache.getCode())) {
                return cache;
            }
        }
        return PhoneExtEnum.OTHER;
    }

    /**
     * 通过code获取联系方式解释名
     * @param code
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/3 22:03
     * @since v1.0
     * @version v1.0
     */
    public static String getNameByCode(String code) {
        if (code == null) {
            return OTHER.getName();
        }
        for (PhoneExtEnum cache : values()) {
            if (Objects.equals(code, cache.getCode())) {
                return cache.getName();
            }
        }
        return OTHER.getName();
    }

    /**
     * 通过联系方式解释名获取code
     * @param name
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/3 22:03
     * @since v1.0
     * @version v1.0
     */
    public static String getCodeByName(String name) {
        if (name == null) {
            return OTHER.code;
        }
        for (PhoneExtEnum cache : values()) {
            if (Objects.equals(name, cache.getName())) {
                return cache.code;
            }
        }
        return OTHER.code;
    }


    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }
}
